
package com.wiesloch.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.wiesloch.po.Distributor;
import com.wiesloch.po.Goods;
import com.wiesloch.po.Page;
import com.wiesloch.po.PageBean;
import com.wiesloch.po.Repertory;
import com.wiesloch.service.DistributorService;
import com.wiesloch.service.GoodsService;
import com.wiesloch.service.RepertoryService;

/**
 * <p>ClassName:DistributorController</p>
 * <p>Description:分销商管理的Controller</p>
 * <p> E-mail:jintao_iknow@163.com </p>
 * Date:     2016年5月10日 下午5:32:06 <br/>
 * @author   tao95
 * @version  1.0
 * @since    JDK 1.7
 *
 */
@Controller
@RequestMapping("/distributor")
public class DistributorController {
	
	@Autowired
	private DistributorService distributorService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private RepertoryService repertoryService;
	private Integer pageSize = 3;
	
	/**
	 * @description 管理员登录
	 * @param request
	 * @param distributor
	 * @param checkbox
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/login",method = { RequestMethod.POST, RequestMethod.GET })
	public void login(HttpServletRequest request,Distributor distributor,String checkbox,HttpServletResponse response) throws Exception{
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		Cookie cookieN = new Cookie("loginName1111", null);
		Map<String, Object> map = new HashMap<String, Object>();
		String account = distributor.getDistributor_account();
		String password = distributor.getDistributor_password();
		HttpSession session = request.getSession();
		System.out.println("checked =="+checkbox+"................."+distributor);
		Distributor distributorSelect = distributorService.selectDistributor(distributor);
		System.out.println(distributorSelect);
		System.out.println(distributorSelect);
		if (distributorSelect == null) {
			map.put("tip", "error");
			map.put("error", "用户名未注册");
		} else {
			System.out.println(1);
			if (!password.equals(distributorSelect.getDistributor_password())) {
				map.put("tip", "error");
				map.put("error", "用户名或者密码错误");
			} else {
				System.out.println(2);
				if (distributorSelect.getDistributor_status() == '1') {
					map.put("tip", "error");
					map.put("error", "用户被冻结");
					
				} else {
					System.out.println(3);
					if (checkbox != null && checkbox.equals("true")) {
						cookieN.setValue(account);
						cookieN.setMaxAge(60 * 10);
						response.addCookie(cookieN);
					} else {
						System.out.println(4);
						response.addCookie(cookieN);
					}
					System.out.println("success");
					map.put("tip", "success");
					session.setAttribute("distributor", distributorSelect);
				}
			}
		}
		String json = JSON.toJSONString(map);
		pw.write(json);
	}
	
	/**
	 * @description 跳转登录界面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loginView",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView loginView(HttpServletRequest request) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/distributor/login");
		return modelAndView;
	}
	
	/**
	 * @description 分页查询分销商列表
	 * @param request
	 * @param pid
	 * @param pageNum
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/distributorList",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView distributorList(HttpServletRequest request,Integer pid,Integer pageNum) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		HttpSession session = request.getSession();
		Distributor distributor = (Distributor) session.getAttribute("distributor");
		List<Distributor> resultList;
		PageBean pageBean ;
		Distributor parent ;
		Page page = new Page();
		
		
		if(pageNum == null){
			pageNum = 1;
		}
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		page.setFirstResult((pageNum - 1)*pageSize);
		if( pid == null){
			parent = distributor;
			Integer id = distributor.getDistributor_id();
			page.setPid(id);
			pageBean = distributorService.selectChildrenListBypid(page);
		}else{
			parent = distributorService.selectDistributorById(pid);
			page.setPid(pid);
			pageBean = distributorService.selectChildrenListBypid(page);
			
		}
		modelAndView.addObject("resultList", pageBean.getRecordList());
		modelAndView.addObject("parent", parent);
		modelAndView.addObject("pageBean", pageBean);
		modelAndView.setViewName("distributor/list");
		return modelAndView;
	}
	
	/**
	 * 
	 * @description 根据id删除该分销商
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteById",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView deleteById(HttpServletRequest request,Integer id) throws Exception{
		
		distributorService.deleteById(id);
		ModelAndView modelAndView = distributorList(request,null,null);
		return modelAndView;
		
		
	}
	
	/**
	 * @description 跳转到添加分销商界面
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addDistributorUI",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView addDistributorUI(HttpServletRequest request,Integer id) throws Exception{
		
		ModelAndView modelAndView = new ModelAndView();
		List<Distributor> provinceList = distributorService.slectDistributorProvince();
		for (Distributor distributor : provinceList) {
			System.out.println(distributor);
		}
		
		modelAndView.addObject("provinceList", provinceList);
		modelAndView.addObject("distributorAdd", null);
		modelAndView.setViewName("distributor/saveUI");
		return modelAndView;
	}
	
	/**
	 * @description 跳转到修改分销商的界面
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/editDistributorUI",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView editDistributorUI(HttpServletRequest request,Integer id)  throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		Distributor distributorAdd = distributorService.selectDistributorById(id);
		modelAndView.addObject("distributorAdd", distributorAdd);
		modelAndView.setViewName("distributor/saveUI");
		return modelAndView;
		
	}
	
	
	/**
	 * @description 添加分销商
	 * @param request
	 * @param distributorAdd
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addDistributor",method = 
		{ RequestMethod.POST, RequestMethod.GET })
	public ModelAndView addDsitributor(HttpServletRequest request,Distributor distributorAdd)throws Exception{
		
		ModelAndView modelAndView = new ModelAndView();
		Integer lastId = distributorService.addDistributor(distributorAdd);
		System.out.println(distributorAdd);
		List<Goods> goodsList = goodsService.selectAllGoods();
		List<Repertory> repertoryList = new ArrayList<Repertory>();
		for (Goods goods : goodsList) {
			Repertory repertory = new Repertory();
			repertory.setRepertory_distributor_id(distributorAdd.getDistributor_id());
			repertory.setRepertory_distributor_pid(distributorAdd.getDistributor_pid());
			repertory.setRepertory_goods_id(goods.getGoods_id());
			repertoryList.add(repertory);
		}
		repertoryService.insertRepertory(repertoryList);
		modelAndView = distributorList(request,null,null);
		return modelAndView;
	}
	
	/**
	 * @description 修改分销商
	 * @param request
	 * @param distributor
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/editDistributor",method = 
		{ RequestMethod.POST, RequestMethod.GET })
	public ModelAndView updateDistributor(HttpServletRequest request,Distributor distributor)throws Exception{
		request.setCharacterEncoding("UTF-8");  
		ModelAndView modelAndView = new ModelAndView();
		distributorService.updateDistributorById(distributor);
		System.out.println(distributor);
		modelAndView = distributorList(request,null,null);
		return modelAndView;
	}
}
